package com.wyvoid.forum.web.tag;

import com.wyvoid.forum.domain.Forum;
import com.wyvoid.forum.service.IForumService;
import com.wyvoid.forum.util.ForumUtil;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * User: cjp
 * Date: 2007-7-4
 * Time: 19:46:15
 */
public class ForumListTag extends TagSupport {
    public int doStartTag() throws JspException {
        String contextPath=((HttpServletRequest)pageContext.getRequest()).getContextPath();
        IForumService forumService = (IForumService) ForumUtil.context.getBean("forumService");
        List<Forum> oneDegree = forumService.getForums(0);

        /*String str = "<select onchange=\"gotoForum(this)\">\n" +
                "    <option value=\"\">论坛跳转 ...</option>";*/
        String str="";
        for (Forum oneForum : oneDegree) {
            str += " <optgroup label=" + oneForum.getName() + ">";
            List<Forum> twoDegree = forumService.getForums(oneForum.getId());
            for (Forum twoForum : twoDegree) {
                str += "<option value=" + twoForum.getId() + ">&nbsp; &gt; □-" + twoForum.getName() + "</option>";
                List<Forum> threeDegree = forumService.getForums(twoForum.getId());
                for (Forum threeForum : threeDegree) {
                    str += "<option value="+threeForum.getId()+">&nbsp; &nbsp; &nbsp; &gt;"+threeForum.getName()+"</option>";
                }
            }

            str += "</optgroup>";
        }

        str += "</select>";
        try {
            pageContext.getOut().write(str);

            /*String script="<script type=\"text/javascript\">\n" +
                    "    function gotoForum(sel) {\n" +
                    "        if (sel.options[sel.selectedIndex].value != '')\n" +
                    "            window.location = ('"+contextPath+"/forum.do?forumId=' + sel.options[sel.selectedIndex].value)\n" +
                    "    }\n" +
                    "</script>";
            pageContext.getOut().write(script);*/
            return SKIP_BODY;
        }
        catch (IOException ioe) {
            throw new JspException(ioe.getMessage());
        }

    }
}
